package renderers 
{
	import entities.Entity;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	import graphics.IGraphic;
	/**
	 * ...
	 * @author Gareth Williams
	 */
	public class EntityRenderer extends Renderer
	{
		public function EntityRenderer(stage:Stage) 
		{
			super(stage);
		}
		override public function destructor():void
		{
			while (stage.numChildren > 0)
			{
				renderables[stage.getChildAt(0)] = null;
				stage.removeChildAt(0);
			}
			super.destructor();//muhahaha
		}
		override public function render():void
		{
			for (var r:Object in renderables) //naughty cast down to object
			{
				if (renderables[r] == false) continue;
				
				var renderable:IRenderable = IRenderable(r);//and cast back to renderable
				
				if (false == renderable.doRender) continue;
				
				var graphic:IGraphic = renderable.getGraphic();
				var entity:Entity = Entity(renderable);
				graphic.move(entity as Point);
				graphic.rotate(entity.rotation);
			}
		}
		override public function addRenderable(renderable:IRenderable):void
		{
			if (renderable is Entity)
			{
				super.addRenderable(renderable);
				stage.addChild(renderable.getGraphic() as Sprite);
			}
		}
		override public function removeRenderable(renderable:IRenderable):void
		{
			if (renderable is Entity)
			{
				renderables[renderable] = false;
				stage.removeChild(renderable.getGraphic() as Sprite);
			}
		}
	}
}